package com.carol.lmsbe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carol.lmsbe.common.Result;
import com.carol.lmsbe.dto.*;
import com.carol.lmsbe.service.EquipmentApplicationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 设备申请控制器
 */
@RestController
@RequestMapping("/api/equipment-application")
@RequiredArgsConstructor
@Slf4j
public class EquipmentApplicationController {

    private final EquipmentApplicationService equipmentApplicationService;

    /**
     * 分页查询设备申请列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<EquipmentApplicationDTO>> listApplications(EquipmentApplicationQueryRequest request) {
        try {
            Page<EquipmentApplicationDTO> result = equipmentApplicationService.listApplications(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询设备申请列表失败", e);
            return Result.error("查询设备申请列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取设备申请详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<EquipmentApplicationDTO> getApplicationById(@PathVariable Long id) {
        try {
            EquipmentApplicationDTO result = equipmentApplicationService.getApplicationById(id);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取设备申请详情失败", e);
            return Result.error("获取设备申请详情失败: " + e.getMessage());
        }
    }

    /**
     * 创建设备申请
     */
    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<EquipmentApplicationDTO> createApplication(@Valid @RequestBody EquipmentApplicationRequest request) {
        try {
            EquipmentApplicationDTO result = equipmentApplicationService.createApplication(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("创建设备申请失败", e);
            return Result.error("创建设备申请失败: " + e.getMessage());
        }
    }

    /**
     * 更新设备申请
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<EquipmentApplicationDTO> updateApplication(@PathVariable Long id, @Valid @RequestBody EquipmentApplicationUpdateRequest request) {
        try {
            request.setId(id);
            EquipmentApplicationDTO result = equipmentApplicationService.updateApplication(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("更新设备申请失败", e);
            return Result.error("更新设备申请失败: " + e.getMessage());
        }
    }

    /**
     * 删除设备申请
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Void> deleteApplication(@PathVariable Long id) {
        try {
            equipmentApplicationService.deleteApplication(id);
            return Result.success();
        } catch (Exception e) {
            log.error("删除设备申请失败", e);
            return Result.error("删除设备申请失败: " + e.getMessage());
        }
    }

    /**
     * 审批设备申请
     */
    @PostMapping("/{id}/approve")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<EquipmentApplicationDTO> approveApplication(@PathVariable Long id, @Valid @RequestBody EquipmentApplicationApprovalRequest request) {
        try {
            request.setApplicationId(id);
            EquipmentApplicationDTO result = equipmentApplicationService.approveApplication(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("审批设备申请失败", e);
            return Result.error("审批设备申请失败: " + e.getMessage());
        }
    }

    /**
     * 获取我的申请列表
     */
    @GetMapping("/my-applications")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<EquipmentApplicationDTO>> getMyApplications(EquipmentApplicationQueryRequest request) {
        try {
            Page<EquipmentApplicationDTO> result = equipmentApplicationService.getMyApplications(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取我的申请列表失败", e);
            return Result.error("获取我的申请列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取待审批申请列表
     */
    @GetMapping("/pending")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<EquipmentApplicationDTO>> getPendingApplications(EquipmentApplicationQueryRequest request) {
        try {
            Page<EquipmentApplicationDTO> result = equipmentApplicationService.getPendingApplications(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取待审批申请列表失败", e);
            return Result.error("获取待审批申请列表失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除申请
     */
    @DeleteMapping("/batch")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<Void> batchDeleteApplications(@RequestBody List<Long> ids) {
        try {
            equipmentApplicationService.batchDeleteApplications(ids);
            return Result.success();
        } catch (Exception e) {
            log.error("批量删除申请失败", e);
            return Result.error("批量删除申请失败: " + e.getMessage());
        }
    }

    /**
     * 导出申请数据
     */
    @GetMapping("/export")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<byte[]> exportApplicationData(EquipmentApplicationQueryRequest request) {
        try {
            byte[] result = equipmentApplicationService.exportApplicationData(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("导出申请数据失败", e);
            return Result.error("导出申请数据失败: " + e.getMessage());
        }
    }
}
